export const baseUrl = 'https://miui.cn1.utools.club'

// 请求封装
export const request = (obj, api, method, other) => {
	let token = uni.getStorageSync('userInfo').token || 'null'
	let url = (other ? '' : baseUrl) + api; //地址
	let data = obj.data || {};
	let success = obj.success;
	let fail = obj.fail || console.log;
	let complete = obj.complete;
	let header = obj.header || {}
	header.token = token
	header['Content-Type'] = !header.json ?
		'application/x-www-form-urlencoded; charset=UTF-8' :
		'application/json; charset=UTF-8'; //
	uni.request({
		url,
		data,
		method,
		header,
		success: (res) => {
			success(res)
		},
		fail: err => {
			fail(err)
		},
		complete: (data) => {
			if (complete) complete(data)
		}
	})
}

export const synchro = (obj, api, method, other) => {
	return new Promise((resolve, reject) => {
		request({
			url: obj.url,
			data: obj.data,
			method: obj.method,
			header: obj.header,
			success: (res) => {
				resolve(res)
			},
			fail: err => {
				reject(err)
			}
		}, api, method, other);
	});
}
